package com.zhangye.test.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhangye.test.mapper.CategoryMapper;
import com.zhangye.test.model.Category;
import com.zhangye.test.util.PageInfo;


/**
 * 分类
 * 作者： 张叶
 * 创建时间：2018年8月21日  下午4:24:56
 */
@Service
public class CategoryService {

	@Autowired
	private CategoryMapper categoryMapper;
	
	public List<Category> selectByWhere(String where){
		List<Category> list = categoryMapper.selectByWhere(where);
		return list;
	}
	
	/**
	 * 分类
	 * 作者： 张叶
	 * 创建时间：2018年8月22日  下午5:00:10
	 */
	public List<Map<String,Object>> selectByPage(PageInfo pageInfo, String where, String order) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("myCount", pageInfo.getCount());
		param.put("pageSize", pageInfo.getPageSize());
		param.put("page", pageInfo.getPage());
		param.put("where", where);
		param.put("order", order);
		List<Map<String, Object>> m = categoryMapper.selectByPage(param);
		pageInfo.setCount(Integer.parseInt(param.get("myCount").toString()));
		return m;
	}
	
	public int insert(Category c){
		int i = categoryMapper.insert(c);
		return i;
	}
	
	
	public int updateByPrimaryKey(Category c){
		int i = categoryMapper.updateByPrimaryKey(c);
		return i;
	}
	
	public Category selectByPrimaryKey(Long id){
		Category c = categoryMapper.selectByPrimaryKey(id);
		return c;
	}
	
	public int deleted (Long id){
		int i = categoryMapper.deleteByPrimaryKey(id);
		return i;
	}
	
	public Category selectByCode(String code){
		List<Category> list = categoryMapper.selectByWhere(" and code = '" + code + "'");
		if(list.size() > 0){
			return list.get(0);
		}else{
			return null;
		}
	}
}
